package com.lizx.persional.common.interceptor;

/**
 * @Author 江苏博沣
 * @Description
 * @Date Create in 2018/2/28 9:51
 */

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class DataLoadingInterceptor implements HandlerInterceptor {
    public DataLoadingInterceptor() {
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            if (handlerMethod.getBean() instanceof HandlerInterceptor) {
                return ((HandlerInterceptor)handlerMethod.getBean()).preHandle(request, response, handler);
            }
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            if (handlerMethod.getBean() instanceof HandlerInterceptor) {
                ((HandlerInterceptor)handlerMethod.getBean()).postHandle(request, response, handler, modelAndView);
            }
        }

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            if (handlerMethod.getBean() instanceof HandlerInterceptor) {
                ((HandlerInterceptor)handlerMethod.getBean()).afterCompletion(request, response, handler, ex);
            }
        }

    }
}
